
package com.hd.sup.core.rest.client;

import com.hd.common.utils.RestUtils;
import com.hd.sup.config.properties.AuthProperties;
import com.hd.sup.modules.enterprise.model.Enterprise;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title TransRestClient.java
 * @Description Auth权限系统远程调用工具
 * @Package com.hd.sup.core.rest.client
 * @date 2021/1/13 5:52 下午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
@Component
public class AuthRestClient {

    public static final String AUTHENTICATION = "Authentication";

    @Resource
    private AuthProperties authProperties;

    /**
     * @Description 请求头
     * @Author chentl
     * @Create: 2021/1/13 5:53 下午
     */
    public static Map<String, Object> headerMap = new HashMap<>();

    /**
     * @Description CONTENT_TYPE
     * @Author chentl
     * @Create: 2021/1/13 5:53 下午
     */
    public static final String CONTENT_TYPE = "Content-Type";
    public static final String APPLICATION_JSON = "application/x-www-form-urlencoded";
    public static final String TENANT = "tenant";
    public static final String HD = "hd";

    static {
        headerMap.put(CONTENT_TYPE, APPLICATION_JSON);
        headerMap.put(TENANT, HD);
    }

    /**
     * @return java.lang.String
     * @author chentl
     * @description 保存权限系统企业数据
     * @date 2021/1/26 12:05 下午
     * @params [enterprise]
     */
    public String saveEnterpriseToAuth(Enterprise enterprise) {
        log.info("[rest] 调用 权限系统添加企业API");
        Long startTime = System.currentTimeMillis();
        String url = authProperties.getPath() + "/dept";
        String result = "";
        try {
            headerMap.put(AUTHENTICATION, authProperties.getStaticToken());
            MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
            param.add("enterprise_name", enterprise.getName());
            param.add("enterprise_no", enterprise.getEnterpriseNo());
            param.add("contactor", enterprise.getContactor());
            param.add("contactor_phone", enterprise.getContactorPhone());
            param.add("industry", enterprise.getIndustry());
            param.add("issue_office", enterprise.getIssueOffice());
            param.add("address", enterprise.getAddress());
            param.add("deptName", enterprise.getName());
            param.add("isboot", "0");
            param.add("parentId", enterprise.getParentId());
            param.add("description", enterprise.getRegion());
            param.add("projectType", "1");

            result = RestUtils.postHttpEncryption(url, param, headerMap);
            Long end = System.currentTimeMillis();
            log.info("[rest] 调用成功，返回值：{}，花费时间: {} ms", result, end - startTime);
        } catch (Exception e) {
            log.error("[rest] 调用 企业基础信息API 时发生异常，{}", result == null ? "接口返回结果为空" : e.getMessage());
        }
        return result;


    }


}
